#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\instruments\makecms.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Instruments {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IMakeCms
	public ref class CMakeCms  : 
            public Cephei::QL::Instruments::IMakeCms
	{
	protected: 
		boost::shared_ptr<QuantLib::MakeCms>* _ppMakeCms;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::MakeCms>* _phMakeCms;
#endif
		Object^ _MakeCmsOwner;     // reference to object that manages the storage for this object
	internal:
		CMakeCms (Cephei::QL::Times::IPeriod^ swapTenor, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Microsoft::FSharp::Core::FSharpOption<Double>^ iborSpread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ forwardStart);
		CMakeCms (Cephei::QL::Times::IPeriod^ swapTenor, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Cephei::QL::Indexes::IIborIndex^ iborIndex, Microsoft::FSharp::Core::FSharpOption<Double>^ iborSpread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ forwardStart);
        CMakeCms (boost::shared_ptr<QuantLib::MakeCms>& childNative, Object^ owner);
        CMakeCms (QuantLib::MakeCms& childNative, Object^ owner);
        CMakeCms (CMakeCms^ copy);
        CMakeCms (PLATFORM::Type^ t);
#ifdef STRUCT
        CMakeCms (QuantLib::MakeCms childNative);
#endif       
#ifdef HANDLE
		CMakeCms (QuantLib::Handle<QuantLib::MakeCms>& childNative, Object^ owner);
		CMakeCms (QuantLib::Handle<QuantLib::MakeCms> childNative);
#endif
		virtual ~CMakeCms ();
		!CMakeCms ();

	internal:
		QuantLib::MakeCms& GetReference ();
		boost::shared_ptr<QuantLib::MakeCms>& GetShared ();
		QuantLib::MakeCms* GetPointer ();
        void SetMakeCms (boost::shared_ptr<QuantLib::MakeCms> native)
        {
            if (_ppMakeCms != NULL)
                delete _ppMakeCms;
            _ppMakeCms = new boost::shared_ptr<QuantLib::MakeCms> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::MakeCms>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Cephei::QL::Instruments::IMakeCms^ ReceiveCms (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithAtmSpread (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsCouponPricer (Cephei::QL::Cashflows::ICmsCouponPricer^ couponPricer) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegCalendar (Cephei::QL::Times::ICalendar^ cal) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegConvention (QL::Times::BusinessDayConventionEnum bdc) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegDayCount (Cephei::QL::Times::IDayCounter^ dc) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegEndOfMonth (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegFirstDate (DateTime d) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegNextToLastDate (DateTime d) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegRule (QL::Times::DateGeneration::RuleEnum r) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegTenor (Cephei::QL::Times::IPeriod^ t) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithCmsLegTerminationDateConvention (QL::Times::BusinessDayConventionEnum bdc) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithDiscountingTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ discountingTermStructure) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithEffectiveDate (DateTime effectiveDate) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegCalendar (Cephei::QL::Times::ICalendar^ cal) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegConvention (QL::Times::BusinessDayConventionEnum bdc) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegDayCount (Cephei::QL::Times::IDayCounter^ dc) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegEndOfMonth (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegFirstDate (DateTime d) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegNextToLastDate (DateTime d) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegRule (QL::Times::DateGeneration::RuleEnum r) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegTenor (Cephei::QL::Times::IPeriod^ t) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithFloatingLegTerminationDateConvention (QL::Times::BusinessDayConventionEnum bdc) ;
		virtual Cephei::QL::Instruments::IMakeCms^ WithNominal (Double n) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Instruments::IMakeCms^>::typeid)]
	[FactoryFor(Cephei::QL::Instruments::IMakeCms::typeid)]
	[FactoryFor(Cephei::QL::Instruments::IMakeCms_Factory::typeid)]
	public ref class CMakeCms_Factory sealed : public IMakeCms_Factory
	{
	public:
        virtual IMakeCms^ Create (Cephei::QL::Times::IPeriod^ swapTenor, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Microsoft::FSharp::Core::FSharpOption<Double>^ iborSpread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ forwardStart);
        virtual IMakeCms^ Create (Cephei::QL::Times::IPeriod^ swapTenor, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Cephei::QL::Indexes::IIborIndex^ iborIndex, Microsoft::FSharp::Core::FSharpOption<Double>^ iborSpread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ forwardStart);
    };
   
/*Cephei*/ } /*QL*/ } /*Instruments */}
